# Project name
PROJECT(stm32-led)
# cmake -G Ninja ../led/ -DCMAKE_TOOLCHAIN_FILE=../led/stm32.cmake
CMAKE_MINIMUM_REQUIRED(VERSION 3.1)
ENABLE_LANGUAGE(ASM)


MESSAGE(STATUS "Project dir is ${PROJECT_SOURCE_DIR}")
#
## source files
# specific folders
SET(U_CMSIS_DIR "${PROJECT_SOURCE_DIR}/../common/cmsis")
SET(U_STM_LIB_DIR "${PROJECT_SOURCE_DIR}/../common/STM32F2xx_StdPeriph_Driver")
MESSAGE("CMSIS folder:${U_CMSIS_DIR}")
MESSAGE("STM Lib folder:${U_STM_LIB_DIR}")

SET(U_CURRENT_SOURCE_FILES )
# libs
FILE(GLOB U_TMP_FILES "${U_STM_LIB_DIR}/src/*.c")
LIST(APPEND U_CURRENT_SOURCE_FILES "${U_TMP_FILES}")

# arch files
LIST(APPEND U_CURRENT_SOURCE_FILES "${PROJECT_SOURCE_DIR}/device/system_stm32f2xx.c")
LIST(APPEND U_CURRENT_SOURCE_FILES "${PROJECT_SOURCE_DIR}/device/stm32f2xx_it.c")
LIST(APPEND U_CURRENT_SOURCE_FILES "${PROJECT_SOURCE_DIR}/device/startup_stm32f2xx.s")
# user files
# bsp group
FILE(GLOB U_TMP_FILES "${PROJECT_SOURCE_DIR}/bsp/*.c")
LIST(APPEND U_CURRENT_SOURCE_FILES "${U_TMP_FILES}")
# src group
FILE(GLOB U_TMP_FILES "${PROJECT_SOURCE_DIR}/src/*.c")
LIST(APPEND U_CURRENT_SOURCE_FILES "${U_TMP_FILES}")

# components
LIST(APPEND U_CURRENT_SOURCE_FILES "${PROJECT_SOURCE_DIR}/components/debug.c")
# MESSAGE(STATUS "files :${U_CURRENT_SOURCE_FILES}")

# header files
SET(U_CURRENT_HEAD_DIR)
LIST(APPEND U_CURRENT_HEAD_DIR "${U_STM_LIB_DIR}/inc")
LIST(APPEND U_CURRENT_HEAD_DIR "${U_CMSIS_DIR}/Core/Include")
# workspace
LIST(APPEND U_CURRENT_HEAD_DIR "${PROJECT_SOURCE_DIR}/device")
LIST(APPEND U_CURRENT_HEAD_DIR "${PROJECT_SOURCE_DIR}/include")
LIST(APPEND U_CURRENT_HEAD_DIR "${PROJECT_SOURCE_DIR}")
# os
LIST(APPEND U_CURRENT_HEAD_DIR "${PROJECT_SOURCE_DIR}/../os/freertos/config")
LIST(APPEND U_CURRENT_HEAD_DIR "${PROJECT_SOURCE_DIR}/../os/freertos/os/include")
LIST(APPEND U_CURRENT_HEAD_DIR "${PROJECT_SOURCE_DIR}/../os/freertos/os/portable/GCC/ARM_CM3")
# MESSAGE(STATUS "headers :${U_CURRENT_HEAD_DIR}")

INCLUDE_DIRECTORIES(${U_CURRENT_HEAD_DIR})

# additional options

# additional cflags
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DSTM32F10X_MD -DUSE_STDPERIPH_DRIVER -O0 -g -gstabs+")

M_GENERATE_MAP(${CMAKE_PROJECT_NAME})
M_ADD_LINK_FILE("${PROJECT_SOURCE_DIR}/device/stm32f207zet6_internal.ld")

MESSAGE(STATUS "----------------------------------------")
MESSAGE(STATUS "C FLAGS:${CMAKE_C_FLAGS}")
MESSAGE(STATUS "CXX FLAGS:${CMAKE_CXX_FLAGS}")
MESSAGE(STATUS "ASM FLAGS:${CMAKE_ASM_FLAGS}")
MESSAGE(STATUS "LINK FLAGS:${CMAKE_EXE_LINKER_FLAGS}")
MESSAGE(STATUS "----------------------------------------")

# subtree
ADD_SUBDIRECTORY("${PROJECT_SOURCE_DIR}/../os/rtx" "rtx-os.out")
ADD_SUBDIRECTORY("${PROJECT_SOURCE_DIR}/../os/freertos" "freertos-os.out")

# lib
LINK_LIBRARIES("freertos")
## target
ADD_EXECUTABLE(${CMAKE_PROJECT_NAME}.elf ${U_CURRENT_SOURCE_FILES})
M_GENERATE_BIN_HEX(${CMAKE_PROJECT_NAME})